<template>
  <div>
    <search-tt @reset="reset" @search='search' :disabled='searchDisabled' :typeInfo='isMyProject?typeInfoM:typeInfo' :years='years' @statusChange='yearChange'>
      <template #right >
        <el-button type="primary" @click="toCreateProject" v-if="hasAuth('sys:project:publish')">新增项目</el-button>
      </template>
      <template #select v-if="hasAuth('sys:project:addBtn')">
        <el-select @change="statusChange" style="width: 160px;" v-model="isMyProject" placeholder="请选择项目类型">
          <el-option
            label="创建的项目"
            :value="0">
          </el-option>
          <el-option
            label="我的项目"
            :value="1">
          </el-option>
          </el-select>
      </template>
    </search-tt>
    <el-table v-loading='loading' :infinite-scroll-disabled="disabled" v-infinite-scroll="load" stripe border :data="projectList" infinite-scroll-delay='1000' style="width: 100%" row-key="menuId">
      <el-table-column label="序号" align="center" type="index" width="70" center></el-table-column>
      <el-table-column label="项目名称"  prop="title"></el-table-column>
      <el-table-column label="项目类型" v-if="isMyProject===''">
        <template slot-scope='scope'>
          <span>{{scope.row.isMyProject?'我的项目':'发布的项目'}}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="开始时间" align="center" prop="startTime" ></el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime"></el-table-column>
      <el-table-column  label="发布时间" prop="createTime" align="center" ></el-table-column>
      <!-- <el-table-column label="任务数" align="center" prop="taskNum" width="90"></el-table-column> -->
      <el-table-column sortable label="状态" v-if="" align="center" prop="status" width="90"></el-table-column>
      <!-- <el-table-column label="当前评分"  v-if="isMyProject" width="100">
        <template slot-scope='scope'>
          <span>{{scope.row.result.score?scope.row.result.score:'当前未考核'}}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" >
        <template slot-scope="scope">
          <el-popconfirm v-if="hasAuth('sys:project:publish')&&!scope.row.isMyProject&&scope.row.status=='未发布'" title="是否确认发布" @confirm="release(scope.row)">
            <el-button type="text" slot="reference">发布</el-button>
          </el-popconfirm>
          <!-- <el-button v-if="hasAuth('sys:project:edit')&&!scope.row.isMyProject&&scope.row.status=='未发布'" type="text"  @click="editProject(scope.row.id)">编辑</el-button> -->
          <el-button v-if="hasAuth('sys:project:creationStream')&&!scope.row.isMyProject&&scope.row.status!='已发布'" type="text" @click="establishFlow(scope)">{{scope.row.flowId?'重新创建流程':'创建流程'}}</el-button>
          <el-button v-if="scope.row.isMyProject&&(scope.row.status=='未完成'||scope.row.status=='已打回')" type="text" @click="toAssess(scope.row)">开始考核</el-button>
          <el-button v-if="scope.row.isMyProject&&(scope.row.status!='未完成'&&scope.row.status!='已打回')" @click="toDetail(scope.row.id)" type="text" >查看详情</el-button>
          <el-button v-if="(scope.row.status!='未完成'||scope.row.status!='未发布'||scope.row.flowId)" @click="openFlowDialog(scope.row.id)" type="text" >查看流程</el-button>
          <el-popconfirm v-if="hasAuth('sys:project:publish')&&!scope.row.isMyProject&&scope.row.status!='已发布'" title="是否确认删除" @confirm="removeProject(scope.row.id)">
            <el-button style="color: red;" type="text" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
      <template slot="empty">
        {{loading?'加载中':'暂无数据'}}
      </template>
    </el-table>
    <flow-details @close='flowDialog=false' v-if="flowDialog" :isMyProject='isMyProject' :flowDialog='flowDialog' :flowDetailInfo='flowDetailInfo'></flow-details>
  </div>
</template>

<script>
  import searchTt from "@/components/searchTt.vue"
  import flowDetails from "@/components/flowDetails.vue"
  export default{
    data(){
      return{
        projectList:[],
        visibleFlag: false,
        value:'',
        typeInfo:{
          title:'',
          status:'',
          page:1,
          limit:10,
          year:'',
        },
        typeInfoM:{
          title:'',
          status:'',
          page:1,
          limit:10,
          year:'',
        },
        loading:true,
        disabled:false,
        flowDialog:false,
        flowDetailInfo:null,
        years:[],
        isMyProject:1,
        searchDisabled:false
      }
    },
    components:{searchTt,flowDetails},
    methods:{
      async initProjectList(){
        let resultM,result
        if(this.isMyProject===''||this.isMyProject==0)result=await this.$get('/api/project/list',this.typeInfo)
        if(this.isMyProject===''||this.isMyProject==1)resultM=await this.$get('/api/user/project/list',this.typeInfoM)
        if(resultM){
          resultM.data.list.map(item=>{
            switch(item.status){
              case 0:
              item.status='未完成'
              break;
              case 1:
              item.status='已完成'
              break;
              case 2:
              item.status='已过期'
              break;
              case 3:
              item.status='已打回'
              break;
              case 4:
              item.status='待审批'
              break;
            }
            item.isMyProject=1
          })
          this.projectList.push(...resultM.data.list)
        }
        if(result){
          result.data.list.map(item=>{
            item.status=item.status==0?'未发布':(item.status==1?'已发布':'已删除')
            item.isMyProject=0
          })
          this.projectList.push(...result.data.list)
        }
        this.loading=false
        if(this.isMyProject==='')this.disabled=this.projectList.length==result.data.total+resultM.data.total
        else if(this.isMyProject==0)this.disabled=this.projectList.length==result.data.total
        else if(this.isMyProject==1)this.disabled=this.projectList.length==resultM.data.total
        this.searchDisabled=false
      },
      async initSel(){
        let code=this.isMyProject===''?2:(this.isMyProject==0?0:1)
        let str='/api/project/years/'+code
        let result=await this.$get(str)
        if(result.code==200) {
          this.years=result.data
        }
      },
      async del(){
        let  result=await this.$get('/api/project/list')
        result.code==200?this.$message.success('删除成功'):this.$message.error(result.msg)
      },
      // 切换项目状态回调
      statusChange(val){
        this.resetInfo()
        this.isMyProject=val
        this.loading=true
        this.initProjectList()
      },
      reset(){
        this.resetInfo()
        this.isMyProject=''
        this.typeInfoM.year=''
        this.typeInfo.year=''
        this.typeInfoM.title=''
        this.typeInfo.title=''
        this.loading=true
        this.initProjectList()
      },
      yearChange(val){
        this.resetInfo()
        this.loading=true
        if(this.isMyProject==''){
          this.typeInfo.year=val
          this.typeInfoM.year=val
        }
        this.initProjectList()
      },
      //发布项目
      async release(row){
        this.loading=true
        let result=await this.$post('/api/project/publish/'+row.id)
        if(result.code==200){
          this.$message.success('发布成功')
          this.resetInfo()
          this.initProjectList()
          row.status='已发布'
        }else{
          this.$message.error(result.msg)
          this.loading=false
        }
      },
      load(){
        this.typeInfo.page++
        this.typeInfoM.page++
        this.initProjectList()
      },
      resetInfo(){
        this.projectList=[]
        this.typeInfo.page=1
        this.typeInfoM.page=1
      },
      search(){
        this.searchDisabled=true
        this.resetInfo()
        this.initProjectList()
      },
      async removeProject(id){
        let result=await this.$post('/api/delProject/'+id)
        if(result.code==200){
          this.$message.success(result.msg)
          this.resetInfo()
          this.initProjectList()
        }else this.$message.error(result.msg)
      },
      async openFlowDialog(id){
        console.log(this.isMyProject)
        let str=this.isMyProject?'/api/project/flow/':'/api/findFlow/'
        let result=await this.$get(str+id)
        if(result.code==200){
          this.flowDetailInfo=result.data
          this.flowDialog=true
        }
      },
      toDetail(id){
        console.log(id)
        this.$store.commit('removeAnalogueTab',"/home/projectdetails")
        this.$store.commit('setTabs',{
          name: "/home/projectdetails?id="+id+"&flag=user",
          title: "查看项目详情"
        })
        this.$router.push('/home/projectdetails?id='+id+'&flag=user')
      },
      establishFlow(scope){
        this.$bus.$emit('changeRouterLive')
        this.$router.push('/home/flow?id='+scope.row.id+'&flowId='+scope.row.flowId)
      },
      editProject(id){
        this.$store.commit('removeAnalogueTab',"/home/creationProject")
        this.$store.commit('setTabs',{
          name: "/home/creationProject?id="+id,
          title: "新增项目"
        })
        this.$router.push('/home/creationProject?id='+id)
      },
      toCreateProject(){
        this.$store.commit('removeAnalogueTab',"/home/creationProject")
        this.$store.commit('setTabs',{
          name: "/home/creationProject",
          title: "新增项目"
        })
        this.$router.push('/home/creationProject')
      },
      toAssess(row){
        this.$store.commit('removeAnalogueTab',"/home/assess")
        // 触发父组件中的改变路由存活配置方法
        this.$bus.$emit('changeRouterLive')
        // this.$store.commit('setTabs',{
        //   name: "/home/assess?id="+row.id,
        //   title: "考核"
        // })
        this.$router.push({path:'/home/assess',query:{id:row.id}})
      }
    },
    mounted(){
      this.initProjectList()
      this.initSel()
    }
  }
</script>

<style>
</style>
